<template>
  <el-dialog :visible.sync="dialogVisible" :before-close="handleClose" width="40%" :title="title">
    <el-form :model="form" ref="form" label-width="80px" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="区域级别">
            <el-radio-group :disabled="form.id!=''" v-model="form.regionLevel" @change="handregionLevelChange">
              <el-radio label="1">省级(直辖市)</el-radio>
              <el-radio label="2">市级(区)</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用状态">
            <el-radio-group v-model="form.status">
              <el-radio label="Y">启用</el-radio>
              <el-radio label="N">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" v-if="form.regionLevel==='2'">
          <el-form-item label="省级名称" prop="sValue">
            <el-select v-model="form.parentId" style="width: 100%" value="1" placeholder="请选择省级名称" clearable
                       @change="handChange">
              <el-option v-for="item in provinceList"
                         :disabled="item.status == 'N'"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="地区名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入地区名称" show-word-limit maxlength="20"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row style="margin-top: 10px">
      <div class="centered-dialog">
        <el-button type="primary" size="mini" @click="confirmSubmit">提交</el-button>
        <el-button type="danger" size="mini" @click="handleClose">取消</el-button>
      </div>
    </el-row>
  </el-dialog>
</template>

<script>
import {detail, getProvince, save} from "@/api/realEstateRegionalAllocation";

export default {
  name: "add",
  data() {
    return {
      dialogVisible: false,
      title: "新增地区",
      provinceList: [],
      form: {
        id: "",
        parentId: "",
        regionLevel: "1",
        name: "",
        status: "Y",
      },
      rules: {
        name: [
          {required: true, message: "请输入地区名称", trigger: "blur"},
        ],
        parentId: [
          {required: true, message: "请选择区域级别", trigger: "change"},
        ],
      }
    };
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
      this.form = {
        id: "",
        parentId: "",
        regionLevel: "1",
        name: "",
        status: "Y",
      }
      this.$refs.form.clearValidate()
    },
    handChange() {
      if (this.form.regionLevel == '1') {
        this.form.parentId = ""
      }
    },
    handregionLevelChange() {
      if (this.form.regionLevel == '1') {
        this.form.parentId = ""
      }
    },
    confirmSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let params = {
            id: this.form.id,
            parentId: this.form.parentId,
            regionLevel: this.form.regionLevel,
            name: this.form.name,
            status: this.form.status,
          }
          const {code, msg, data} = await save(params);
          if (code == 200) {
            this.$message.success("新增成功")
            this.dialogVisible = false
            this.handleClose()
            await this.$parent.getPage()
          }
        }
      })
    },
    async getProvince() {
      // 获取省级名称
      const {code, msg, data} = await getProvince();
      if (code == 200) {
        this.provinceList = data
      }
    },
    async getDetail(id) {
      // 获取详情
      const {code, msg, data} = await detail({id: id})
      if (code == 200) {
        this.form = data
      }
    }
  }
}
</script>


<style scoped lang="scss">
.centered-dialog {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>